package com.jake.dynamicproxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler {

    private final Object delegate;

    public MyInvocationHandler(Object delegate) {
        this.delegate = delegate;
    }

    public Object createJDKProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(delegate, args);
        after();
        return result;
    }

    private void before() {
        System.out.println("jdk proxy: before method");
    }

    private void after() {
        System.out.println("jdk proxy: after method");
    }
}
